package com.xpgk.handler;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jfinal.handler.Handler;
import com.jfinal.log.Log;
import com.xpgk.constant.ConstantWebContext;
import com.xpgk.tools.ToolRandoms;
import com.xpgk.tools.ToolWeb;

/**
 * 全局Handler，设置一些通用功能 描述：主要是一些全局变量的设置，再就是日志记录开始和结束操作
 */
public class GlobalHandler extends Handler {

	private static final Log log = Log.getLog(GlobalHandler.class);

	@Override
	public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {

		log.debug("设置 web 路径");
		String cxt = ToolWeb.getContextPath(request);
		request.setAttribute(ConstantWebContext.request_cxt, cxt);

		log.debug("request 随机分配一个请求id");
		request.setAttribute(ConstantWebContext.request_id, ToolRandoms.getUuid(true));

		log.debug("request cookie 处理");
		Map<String, Cookie> cookieMap = ToolWeb.readCookieMap(request);
		request.setAttribute(ConstantWebContext.request_cookieMap, cookieMap);

		log.debug("request param 请求参数处理");
		request.setAttribute(ConstantWebContext.request_paramMap, ToolWeb.getParamMap(request));

		log.debug("设置Header");
		/*request.setAttribute("decorator", "none");
		response.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
		response.setHeader("Pragma", "no-cache"); // HTTP 1.0
		response.setDateHeader("Expires", 0); // prevents caching at the proxy
												// server
*/
		next.handle(target, request, response, isHandled);
		log.debug("render complited");
	}
}
